$(document).ready(function() {
	//获取项目名
	var contextPath = document.location.pathname; 
	var index =contextPath.substr(1).indexOf("/"); 
	contextPath = contextPath.substr(0,index+1); 
	
	var totalCount = $("#totalCount").val();
	var pSize = $("#pageSize").val();
	var pageIndex = $("#pageIndex").val();
	var pageCount = $("#pageCount").val();
	
	
	var pareArr = new Array()// 定义一个数组
	var sonArr = new Array()// 定义一个数组
	//大图
	$('#pagination2').jqPaginator({
		/*totalPages : pageCount,
		visiblePages : 12,
		currentPage : 1,*/
		totalCounts:totalCount,
		pageSize: pSize,
	    visiblePages: pageCount,
	    currentPage: 1,
		prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
		page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
		next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
		onPageChange : function(num, type) {
			$("#pageIndex").val(num);
			var queryUrl = "queryByPage2.do";
			$.ajax({
				url : queryUrl,
				dataType : 'json',
				data : {
					pageIndex : num,
					pageSize : pSize
				},
				cache : false,
				success : function(data) {
					//礼品分类
					var $cata = $(".cata-group");
					for(i=0,len=$cata.find("dl").length;i<len;i++){
						var pareId = $cata.find("dl:eq("+i+")").find("dd[index]").attr("index");
						pareArr.push(pareId);
						sonArr.push(pareId);
					}
					
					//礼品基本信息
					$( "#giProd li") .remove();
					var results = data.pageResult.resultList;
					for (i = 0; i < results.length; i++) {
						var pr = results[i];
						//转换null值
						if(pr.promotion==null){
							pr.promotion=0;
						}
						var rowContent = "<li class=''> <div class='prodPic'> " 
							+ "<a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName 
							+ "'> " 
							+ "<img class='lazy' src='"+contextPath+"/HRTWEB/images/23/" 
							+ pr.pic
							+ "' alt='" 
							+ pr.giftName
							+ "' /> </a> </div> <div class='price'>价格：￥" 
							+ pr.promotion
							+ "</div> " 
							+ "<div class='desc'> <a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+ "'>" 
							+ pr.giftName
							+ "</a> </div> 	<div class='original'>积分：" 
							+ pr.promotion
							+ "</div> </li>";
						$("#giProd") .append(rowContent);
						
					}
				},
				error : function(html) {
					return;
				}
			});
		}
	});
	
	
	//列表
	$('#pagination3').jqPaginator({
		totalPages : pageCount,
		visiblePages : 8,
		currentPage : 1,
		prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
		page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
		next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
		onPageChange : function(num, type) {
			$("#pageIndex").val(num);
			var queryUrl = "queryByPage2.do";
			$.ajax({
				url : queryUrl,
				dataType : 'json',
				data : {
					pageIndex : num,
					pageSize : 8
				},
				cache : false,
				success : function(data) {
					//礼品基本信息
					$( "#taGiPro tr") .remove();
					var results = data.pageResult.resultList;
					for (i = 0; i < results.length; i++) {
						var pr = results[i];
						//转换null值
						if(pr.promotion==null){
							pr.promotion=0;
						}
						var rowContent = "<tr> " 
							+ "<td><a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+"'>  " 
							+ "<img class='lazy' src='"+contextPath+"/HRTWEB/images/23/" 
							+ pr.pic
							+ "' alt='" 
							+ pr.giftName
							+ "' width='120px' /> " 
							+ "</a> " 
							+ "</td>" 
							+ "<td>" 
							+ "<div class='desc'> " 
							+ "<a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+ "'>" 
							+ pr.giftName
							+ "</a>" 
							+ "</div>" 
							+ "</td>" 
							+ "<td>" 
							+ "<div class='price'>价格：￥" 
							+ pr.promotion
							+ "<br/>运费：￥10 " 
							+ "</div></td><td><div class='original'>积分：" 
							+ pr.promotion
							+ "</div></td></tr>";
						$("#taGiPro") .append(rowContent);
						}
					
					},
					error : function(html) {
						return;
					}
				});
			}
		});
	

	$("#parentsList").delegate('.cateid', 'click', function() {
		var parentId = $(this).parent().attr("index");
		var sonId = $(this).attr("sonId");
		var num = 0;//计算是否父级id相同
		for(i=0; i<pareArr.length; i++){
			if(parentId==pareArr[i]){
				sonArr[i]=sonId
			}
		}
		
//		alert("pareArr==>"+pareArr);
//		alert("sonArr==>"+sonArr);
		
		//通过Ajax将礼品分类子级的id数组传到后台
		var sonUrl = contextPath+"/HRTWEB/findGiftById.do";
		$.ajax({
			url : sonUrl,
			dataType : 'json',
			data : {
				ids : sonArr.toString(),
				pageIndex : 1,
				pageSize : pSize
			},
			cache : false,
			success : function(data) {
				
				var results = data.typePage.resultList;
				if($("#switchover1").css("display")=='block'){
					//礼品基本信息
					$( "#giProd li") .remove();
					for (i = 0; i < results.length; i++) {
						var pr = results[i];
						//转换null值
						if(pr.promotion==null){
							pr.promotion=0;
						}
						var rowContent = "<li class=''> <div class='prodPic'> " 
							+ "<a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName 
							+ "'> " 
							+ "<img class='lazy' src='"+contextPath+"/HRTWEB/images/23/" 
							+ pr.pic
							+ "' alt='" 
							+ pr.giftName
							+ "' /> </a> </div> <div class='price'>价格：￥" 
							+ pr.promotion
							+ "</div> " 
							+ "<div class='desc'> <a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+ "'>" 
							+ pr.giftName
							+ "</a> </div> 	<div class='original'>积分：" 
							+ pr.promotion
							+ "</div> </li>";
						$("#giProd") .append(rowContent);
						
					}
				}else{
					//礼品基本信息
					$( "#taGiPro tr") .remove();
//					var results = data.typePage.resultList;
					for (i = 0; i < results.length; i++) {
						var pr = results[i];
						//转换null值
						if(pr.promotion==null){
							pr.promotion=0;
						}
						var rowContent = "<tr> " 
							+ "<td><a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+"'>  " 
							+ "<img class='lazy' src='"+contextPath+"/HRTWEB/images/23/" 
							+ pr.pic
							+ "' alt='" 
							+ pr.giftName
							+ "' width='120px' /> " 
							+ "</a> " 
							+ "</td>" 
							+ "<td>" 
							+ "<div class='desc'> " 
							+ "<a href='"+contextPath+"/HRTWEB/details/details.do?id="
							+ pr.id
							+ "' target='_blank' title='" 
							+ pr.giftName
							+ "'>" 
							+ pr.giftName
							+ "</a>" 
							+ "</div>" 
							+ "</td>" 
							+ "<td>" 
							+ "<div class='price'>价格：￥" 
							+ pr.promotion
							+ "<br/>运费：￥10 " 
							+ "</div></td><td><div class='original'>积分：" 
							+ pr.promotion
							+ "</div></td></tr>";
						$("#taGiPro") .append(rowContent);
						}
				}
				
			},
			error : function(html) {
				alert("sonId Data failly");
				return;
			}
		});
		
	});
	
	
	});
